package com.dinggou.shopjb.view;

import static com.dinggou.shopjb.utils.RouteUtil.backActivity;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.dinggou.shopjb.R;
import com.dinggou.shopjb.application.MyApplication;
import com.dinggou.shopjb.bean.UserInfoBean;
import com.dinggou.shopjb.core.AppAction;
import com.dinggou.shopjb.net.ActionCallbackListener;
import com.dinggou.shopjb.utils.DataUtils;
import com.dinggou.shopjb.utils.TimeCount;
import com.dinggou.shopjb.utils.ToastUtil;

public class UnBindWithdrawCardDialog extends MyDialog implements View.OnClickListener {
    private static final String TAG = "SignDialog";
    private View rootView;
    private AppAction mAppAction;
    private TextView tv_mobile,tv_get_code,tv_confirm,tv_cancel;
    private TimeCount timeCount;
    private EditText et_code;

    public UnBindWithdrawCardDialog(Context context, boolean isCancelAble, int gravity) {
        super(context, isCancelAble, gravity);
        this.mContext = context;
        Window w = this.getWindow();
        if (w != null) {
            w.setWindowAnimations(R.style.dialogTranslateAnim);
        }
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_unbind_withdraw_card, null);
        setContentView(rootView);
        findViews(rootView);
        mAppAction = ((MyApplication) mContext.getApplicationContext()).getAppAction();
    }

    private void findViews(View rootView) {
        tv_mobile = rootView.findViewById(R.id.tv_mobile);
        tv_get_code = rootView.findViewById(R.id.tv_get_code);
        tv_confirm = rootView.findViewById(R.id.tv_confirm);
        tv_cancel = rootView.findViewById(R.id.tv_cancel);
        et_code = rootView.findViewById(R.id.et_code);



        UserInfoBean bean =  DataUtils.getUserInfo(mContext);
        String  mobile = bean.mobile;
        String unBindMobile = "解绑需验证手机号码：" + mobile.substring(0,3) + "****" + mobile.substring(7);
        tv_mobile.setText(unBindMobile);
        tv_get_code.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);


        mAppAction =  MyApplication.getApplication().getAppAction();

    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.tv_get_code){
            getCode();
            return;
        }
        if(view.getId() == R.id.tv_confirm){
            submitClick();
            return;
        }
        if(view.getId() == R.id.tv_cancel){
            cancelClick();
            return;
        }

    }
    private void getCode(){

       mAppAction.getUnbindCardCode(new ActionCallbackListener<Void>() {
           @Override
           public void onSuccess(Void data) {
               ToastUtil.showToast(mContext,"发送成功");
               timeCount = new TimeCount(tv_get_code, 60000, 1000, "再次获取");
               timeCount.start();
           }
           @Override
           public void onFailure(String errorCode, String message) {
               ToastUtil.showToast(mContext,message);
           }
       });
    }
    private void submitClick(){
        String smsCode = et_code.getText().toString();
        if(TextUtils.isEmpty(smsCode)) {
            ToastUtil.showToast(mContext, "请输入验证码");
            return;
        }
        mAppAction.unbindCard("1",smsCode, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                dismiss();
                ToastUtil.showToast(mContext, "解绑成功");
                backActivity(mContext);
            }
            @Override
            public void onFailure(String errorCode, String message) {
                dismiss();
                ToastUtil.showToast(mContext, message);
            }

        });
    }
    private void cancelClick(){
        dismiss();
    }
}
